package org.csu.jpetstorespringversion.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.jpetstorespringversion.mapper.UserMapper;
import org.csu.jpetstorespringversion.model.User;
import org.csu.jpetstorespringversion.service.UserService;
import org.springframework.stereotype.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public boolean jiami(Map<String, Object> param, String password) {
        return false;
    }
    public Page<User> listData(User user, long current, long size) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (null != user.getUserid()) {
            queryWrapper.eq("id", user.getUserid());
            queryWrapper.like("firstname", user.getFirstname());
            queryWrapper.like("lastname", user.getLastname());
            queryWrapper.like("addr1", user.getAddr1());
            queryWrapper.like("addr2", user.getAddr2());
            queryWrapper.like("city", user.getCity());
            queryWrapper.like("email", user.getEmail());
            queryWrapper.like("country", user.getCountry());
            queryWrapper.like("state", user.getState());
            queryWrapper.like("favcategory", user.getFavcategory());
            queryWrapper.like("zip", user.getZip());
            queryWrapper.like("phone", user.getPhone());
        }

        return baseMapper.selectPage(new Page<>(current, size), queryWrapper);
    }
    @Override
    public  String encrypt(String strSrc) {
        try {
            char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            byte[] bytes = strSrc.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }
    }
}
